<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入开门人姓名"
        v-model="queryForm.passPersonName"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入开门人手机号"
        v-model="queryForm.passPersonPhone"
      >
      </el-input>
      <el-date-picker
        class="filter-item"
        type="date"
        v-model="queryForm.startTime"
        :picker-options="pickerStartTime"
        placeholder="选择开始日期"
        style="width: 150px;"
        value-format="yyyy-MM-dd"
        @change="changeTime"
      ></el-date-picker>
      <label class="filter-item">-</label>
      <el-date-picker
        class="filter-item"
        type="date"
        v-model="queryForm.endTime"
        :picker-options="pickerEndTime"
        placeholder="选择结束日期"
        style="width: 150px;"
        value-format="yyyy-MM-dd"
      ></el-date-picker>

      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        title="搜索"
        >搜索</el-button
      >
      <el-switch
        v-model="value3"
        active-text="刷新"
        inactive-text="暂停"
        @change="refresh"
      >
      </el-switch>
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="realName" label="用户" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="villageName"
        label="省/市/区"
        width="450"
        align="center"
      >
        <template slot-scope="scope">
          <template
            v-if="
              scope.row.townName !== undefined &&
                scope.row.villageName !== undefined
            "
          >
            {{
              `${scope.row.provinceName}/${scope.row.cityName}/${scope.row.districtName}/${scope.row.townName}/${scope.row.villageName}`
            }}
          </template>
          <template
            v-if="
              scope.row.townName !== undefined &&
                scope.row.villageName === undefined
            "
          >
            {{
              `${scope.row.provinceName}/${scope.row.cityName}/${scope.row.districtName}/${scope.row.townName}`
            }}
          </template>
          <template
            v-if="
              scope.row.townName === undefined &&
                scope.row.villageName === undefined
            "
          >
            {{
              `${scope.row.provinceName}/${scope.row.cityName}/${scope.row.districtName}`
            }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="outInFlag"
        label="出入标识"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.OUT_IN_FLAG[scope.row.outInFlag] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="caName"
        label="摄像机名称"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="passPersonTime"
        label="开门时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="passPersonName"
        label="开门人姓名"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="passPersonPhone"
        label="开门电话"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="isStranger"
        label="是否陌生人"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.IS_STRANGER[scope.row.isStranger] }}
        </template>
      </el-table-column>
      <el-table-column prop="url" label="图片" width="150" align="center">
        <template slot-scope="scope" v-if="scope.row.url != null">
          <div v-viewer>
            <img
              :src="$oss(scope.row.url)"
              alt
              style="width:30px;height:30px;border-radius:5px;"
            />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { queryPersonPassRecord } from '@/api/consumer'
import 'viewerjs/dist/viewer.css'
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)

export default {
  name: 'Consumer',
  data() {
    return {
      value3: false,
      dialogFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: ''
      },
      attributeOptions: Dicts.USER_ATTRIBUTE,
      statusOptions: Dicts.CONSUMER_STATUS,
      records: [],
      pickerStartTime: {
        disabledDate: time => {
          if (this.queryForm.endTime !== '') {
            return (
              time.getTime() > Date.now() ||
              time.getTime() > this.queryForm.endTime
            )
          } else {
            return time.getTime() > Date.now()
          }
        }
      },
      pickerEndTime: {
        disabledDate: time => {
          return time.getTime() < new Date(this.queryForm.startTime).getTime()
        }
      }
    }
  },
  created() {
    // 初始化一次oss client,如果初始化过一次返回上一次初始化对象
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    changeTime(date) {
      this.pickerEndTime = {
        disabledDate(time) {
          return time.getTime() < new Date(date).getTime()
        }
      }
    },
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      queryPersonPassRecord(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
      })
    },
    refresh(value) {
      if (value === true) {
        this.timer = setInterval(() => {
          this.doQuery()
        }, 5000)
      } else {
        clearInterval(this.timer)
      }
    }
  },
  components: {}
}
</script>

<style scoped></style>
